package common.utils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@SuppressWarnings({"UnusedDeclaration"})
public final class ReflectUtil
{

	public static Object callMethod(Object context, String methodName, Object[] args)
			throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
	{
		Class[] argsTypes = getTypes(args);
		Method method = context.getClass().getMethod(methodName, argsTypes);
		return method.invoke(context, args);
	}

	public static Class[] getTypes(Object[] args)
	{
		Class[] argsTypes = new Class[args.length];

		int i = 0;

		for (Object arg : args)
		{
			argsTypes[i++] = (arg == null)
					? Object.class
					: arg.getClass();
		}

		return argsTypes;
	}
}
